class Solution {
   public:
    string countAndSay(int n) {
        string str = "1";
        while (--n) {
            string next = "";
            for (int i = 0, n = str.size(); i < n; i++) {
                char ch = str[i];
                int cnt = 1;
                while (i + 1 < n && str[i + 1] == ch) i++, cnt++;
                while (cnt) {
                    next += cnt % 10 + '0';
                    cnt /= 10;
                }
                next += ch;
            }
            str = next;
        }
        return str;
    }
};